#主要用nonlocal关键字 更改外部变量，函数体内部的临时变量
#在闭包函数中修改外部函数的值
from numpy.ma.core import outer


def foo(a):

    def bar(b):
        nonlocal a
        a = a+b
        print(a)
    return bar
abb=foo(10)
abb(20)


#装饰器,创建一个闭包函数，在闭包函数内部调用目标函数


def outer(func):
    def inner():
        print("程序开始啦")
        func()
        print("程序结束啦")
    return  inner
#outer必须在下面
@outer
def main():
    import random
    print(f'{random.randint(1,5)}')
main()

#单例模式 在一个文件里创建类，并且构造一个类对象
# class Stu:
#     pass
# student=Stu()
#在另一个类中导入这个类对象，可以创建多个对象并且是同一个地址
# from 文件 import student
# s1=student
# s2=student

#工厂模式
class Person:
    pass
class Student(Person):
    pass
class Teacher(Person):
    pass
class Child(Person):
    pass
class Test:
    def select(self,name):
        if name == "w":
            return Student()
        elif name=="t":
            return Teacher()
        elif name=="c":
            return Child()

t1=Test()
student=t1.select("t")
import  time
def sing(msg):
    while True:
        print(msg)
        time.sleep(1)


def dance(msg):
    while True:
        print(msg)
        time.sleep(1)



import threading
if __name__ == '__main__':
    t1 = threading.Thread(target=sing, args=("我超级喜欢你"))
    t2 = threading.Thread(target=dance, kwargs={"label": "我真的超级喜欢"})

    t1.start()
    t2.start()
    
